package com.demo.generator;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;

public class CodeGeneratorSinoits35 {

    /**
     * 数据库连接驱动 不用修改
     */
    private static final String driverName = "com.mysql.cj.jdbc.Driver";

    /**
     * todo 1、修改数据库信息  连接名、账号、密码
     * 数据库地址链接
     */
//    private static final String datasource = "jdbc:mysql://rdsi7ip8c7o4o5j217mu6o.mysql.rds.aliyuncs.com/pepsi_icebox_device?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai";
//    private static final String datasource = "jdbc:mysql://192.168.1.8:13306/group_device_kangshifu_test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull";
//    private static final String datasource =
//            "jdbc:mysql://127.0.0.1:13306/demo?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull";

//     private static final String datasource =
//              "jdbc:mysql://127.0.0.1:3308/hlyy?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull";

    private static final String datasource =
            "jdbc:mysql://192.168.6.35:23306/hlyy?useSSL=false&useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUE&serverTimezone=Asia/Shanghai&allowMultiQueries=true&useInformationSchema=true";

    /**
     * 数据库登录名
     */
//    private static final String login = "root";
    private static final String login = "sinoits";

    /**
     * 数据库登录密码
     */
//    private static final String password = "123456";
    private static final String password = "sinoits@123";

    /**
     * todo 2、修改要生成的表名
     * 要创建代码的数据库表明，多个表用,分割
     */
//    private static final String tableNames = "t_enum_const";
    private static final String tableNames = "t_virtual_resource_manage";
//    private static final String tableNames = "t_virtual_resource_manage";
//    private static final String tableNames = "t_event_continuous";
//    private static final String tableNames = "t_hardware_server_manage";
//    private static final String tableNames = "t_audit_event";
//    private static final String tableNames = "jtl_lxsj_luduan_now";
//    private static final String tableNames = "t_sample_manage";
//    private static final String tableNames = "t_evaluation_schedule";
//    private static final String tableNames = "t_information_database_property_manage";
//    private static final String tableNames = "t_resource_monitor_alert_manage";
//    private static final String tableNames = "t_analysis_task_manage";

    /**
     * todo 3、设定要输出的文件目录
     * 输出文件夹
     */
//    private static final String outputPath = "D://generator/java/"+System.currentTimeMillis();
    private static final String outputPath = "D://generator/";
//    private static final String outputPath = "D://YszxDataStatistics/";

    /**
     * todo 4、设定包名
     * 输出包名
     */
//    private static final String packageInfo = "com.whaty";
    private static final String packageInfo = "com.sinoits.microservicehlyy";

    /**
     * 输出模块名
     */
//    private static final String moduleName = "yszx";
//    private static final String moduleName = "evaluation";
    private static final String moduleName = "test";

    /**
     * 如果模板引擎是 freemarker
     * 如果模板引擎是 velocity
     * String templatePath = "/templates/mapper.xml.vm";
     */
    private static final String mapperXmlTemplatePath = "/templates/mapper.xml.ftl";

    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // java代码输出位置配置,和全局配置
        GlobalConfig gc = initOutput();
        gc.setSwagger2(true); // 实体属性 Swagger2 注解
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = initDatasourceConfig();
        mpg.setDataSource(dsc);

        // 代码包名配置
        PackageConfig pc = initPackageConfig();
        mpg.setPackageInfo(pc);

        // mapper.xml自定义配置
        InjectionConfig cfg = initMapperXmlConfig(pc);
        mpg.setCfg(cfg);

        // 配置模板
        TemplateConfig templateConfig = initTemplateConfig();
        mpg.setTemplate(templateConfig);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());

        // 类相关策略配置
        StrategyConfig strategy = initClassConfig();
        mpg.setStrategy(strategy);

        //执行创建模板
        mpg.execute();
    }

    private static StrategyConfig initClassConfig() {
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
//        strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
//        strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
        // todo 暂时取消自定义父类设置，使用 MP 现有的基类接口
//        strategy.setSuperServiceClass("com.group.parent.database.base.BaseService");
//        strategy.setSuperServiceImplClass("com.group.parent.database.base.BaseServiceImpl");
//        strategy.setSuperMapperClass("com.group.parent.database.base.BaseMapper");

        // 写于父类中的公共字段
//        strategy.setSuperEntityColumns("id");
        strategy.setInclude(tableNames.split(","));
//        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix("t_");

        return strategy;
    }

    private static TemplateConfig initTemplateConfig() {
        TemplateConfig templateConfig = new TemplateConfig();

        // 配置自定义输出模板
        //指定自定义模板路径，注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别  采用 freemarker模板引擎
//         templateConfig.setEntity("templates/entity2.java");
        templateConfig.setServiceImpl("templates/serviceImpl.java");
//        templateConfig.setServiceImpl("templates/serviceImpl_notext.java");
        templateConfig.setController("templates/controller.java");
//        templateConfig.setController("templates/controller_notext.java");

        templateConfig.setXml(null); // mapper.xml 采用了自定义配置

        templateConfig.setMapper("templates/mapper.java");
        return templateConfig;
    }

    private static InjectionConfig initMapperXmlConfig(PackageConfig pc) {
//        InjectionConfig cfg = new InjectionConfig() {
//            @Override
//            public void initMap() {
//                // to do nothing
//            }
//        };
        MyInjectionConfig cfg = new MyInjectionConfig();

        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输出
        focList.add(new FileOutConfig(mapperXmlTemplatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 ， 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化！！
//                return outputPath + "/src/main/resources/mapper/" + pc.getModuleName()
//                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
                return outputPath + "/src/main/resources/mapper/" + pc.getModuleName()
                        + "/" + tableInfo.getEntityName() + "MapperExt" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        return cfg;
    }

    private static PackageConfig initPackageConfig() {
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(moduleName);
        pc.setParent(packageInfo);
        return pc;
    }

    private static DataSourceConfig initDatasourceConfig() {
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(datasource);
        dsc.setDriverName(driverName);
        dsc.setUsername(login);
        dsc.setPassword(password);
        return dsc;
    }

    private static GlobalConfig initOutput() {
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(outputPath + "/src/main/java");
        // todo 修改author
        gc.setAuthor("gy");
        gc.setOpen(false);
        gc.setFileOverride(true);//是否覆盖已经生成的
        gc.setBaseResultMap(true);
        gc.setBaseColumnList(true);
        return gc;
    }

}
